#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from path import Path
from pymongo import MongoClient


# 连接MongoDB
def InitMongoDB(MONGODB_HOST, MONGODB_PORT, MONGODB_DB_NAME, MONGODB_COLLECTION_NAME):
    client = MongoClient(MONGODB_HOST, MONGODB_PORT)
    db = client[MONGODB_DB_NAME]
    collection = db[MONGODB_COLLECTION_NAME]
    return client, collection


# 关闭MongoDB
def CloseMongoDB(MongoDB_Connection):
    MongoDB_Connection.close()


if __name__ == '__main__':

    sourcepath = Path("/local/home/dataadmin/types-genome")
    destinationpath = Path("/local/home/dataadmin/ncbi_assembly")

    client_eLMSG, collection_eLMSG = InitMongoDB("10.188.188.22", 27017, "eLMSG", "eLMSG_1112")

    for results in collection_eLMSG.find({"ncbi_assembly": {"$exists": 1}}, {"_id": 0, "ncbi_assembly": 1}):
        for result in results["ncbi_assembly"]:
            Path.copy(sourcepath.joinpath(result["id"] + ".fna"), destinationpath.joinpath(result["id"]) + ".fna")
            print("Copy file " + result["id"] + ".fna")
    CloseMongoDB(client_eLMSG)
